package com.maven.front.service.frontIndex;

import com.maven.front.bean.*;
import com.maven.front.dto.EduCommentDto;
import com.maven.front.dto.EduCourseStudyHistoryDto;
import com.maven.front.mapper.*;
import com.maven.front.util.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: web-front
 * @description: 前台首页显示
 * @author: WangYiLong
 * @create: 2020-08-11 10:31
 **/
@Service
public class FrontIndexService {

    @Resource
    private EduWebsiteCourseDetailMapper detailMapper;

    @Resource
    private EduCourseMapper eduCourseMapper;

    @Resource
    private EduCourseStudyhistoryMapper eduCourseStudyhistoryMapper;

    @Resource
    private EduCommentMapper eduCommentMapper;

    @Resource
    private EduUserMapper eduUserMapper;

    @Resource
    private EduTeacherMapper eduTeacherMapper;

    //精品课程
    public List<EduCourse> getRecommendCourse()  {
        List<EduWebsiteCourseDetail> list = detailMapper.getList();
        return eduCourseMapper.selectRecommendCourse(list);
    }

    //最新课程
    public List<EduCourse> freshCourse(){
        return eduCourseMapper.freshCourse();
    }

    //全部课程
    public List<EduCourse> selectAll(){
        return eduCourseMapper.select(null);
    }

    //学生动态
    public List<EduCourseStudyHistoryDto> selectByUpdateTime(){
        List<EduCourseStudyHistoryDto> list = new ArrayList<>();
        List<EduCourseStudyhistory> eduCourseStudyhistories = eduCourseStudyhistoryMapper.selectByUpdateTime();
        List<EduUser> users =  eduUserMapper.select(null);
        for(EduCourseStudyhistory h:eduCourseStudyhistories){
            EduCourseStudyHistoryDto dto = new EduCourseStudyHistoryDto();
            BeanUtils.copy(h,dto);
            for(EduUser u:users){
                if(u.getUserId()==h.getUserId()){
                    dto.setEmail(u.getEmail());
                    dto.setPicImg(u.getPicImg());
                    dto.setShowName(u.getShowName());
                    break;
                }
            }
            list.add(dto);
        }
        return list;
    }

    //课程互动
    public List<EduCommentDto> selectCourseInteract(){
        List<EduCommentDto> list = new ArrayList<>();
        List<EduComment> comments = eduCommentMapper.select(null);
        List<EduUser> users =  eduUserMapper.select(null);
        List<EduCourse> courses = eduCourseMapper.select(null);
        for(EduComment c:comments){
            EduCommentDto commentDto = new EduCommentDto();
            //赋值父类属性
            BeanUtils.copy(c,commentDto);
            //添加用户信息
            for(EduUser user:users){
                if(user.getUserId()==c.getUserId()){
                    commentDto.setEmail(user.getEmail());
                    commentDto.setPicImg(user.getPicImg());
                    break;
                }
            }
            //添加课程信息
            for(EduCourse course:courses){
                if(course.getCourseId()==c.getOtherId()){
                    commentDto.setCourseName(course.getCourseName());
                    break;
                }
            }
            list.add(commentDto);
        }
        return list;
    }

    //高级讲师
    public List<EduTeacher> selectByIsStar(){
        return eduTeacherMapper.selectByIsStar();
    }

    /**
     * 首页所需渲染数据
     * @return
     */
    public Map<String,Object> getIndex(){
        Map<String,Object> map = new HashMap<>();
        //精品课程数据
        map.put("recommendCourse",getRecommendCourse());
        //课程互动数据
        map.put("courseInteract",selectCourseInteract());
        map.put("students",selectByUpdateTime());
        map.put("teachers",selectByIsStar());
        return map;
    }
}
